<?php
session_start();
require_once('Connections/conn.php');
require_once('allfunction.php');
function calcDalivery($w,$type)
{
	$w=$w+$_SESSION["curr_deli_bw"];
	$sql="SELECT weight_start, weight_end, price FROM sme_delivery_rate WHERE delivery_ids =$type ORDER BY weight_end ASC";
	$result=mysql_query($sql);
	$num=mysql_num_rows($result);
	if($num<1)return 0;
	$i=0;
	$rate=array();
	while($rs=mysql_fetch_array($result))
	{
		$rate[$i][0]=$rs[0];
		$rate[$i][1]=$rs[1];
		$rate[$i][2]=$rs[2];
		$i++;
	}
	$maxW=$rate[$num-1][1];
	$maxP=$rate[$num-1][2];
	$box=floor($w/$maxW);
	if($box)
    {
		$fullboxP=$box*$maxP;
		$Fraction=$w-($box*$maxW);
		if($Fraction>0)return compareW($Fraction,$rate,$num)+$fullboxP+$_SESSION["curr_deli_sc"];
		else return $fullboxP+$_SESSION["curr_deli_sc"];
	}
	else 
	return	compareW($w,$rate,$num)+$_SESSION["curr_deli_sc"];
}
function compareW($w,$rate,$num)
{
for($i=0;$i<$num;$i++)
	if($w>=$rate[$i][0] && $w<=$rate[$i][1])
		return $rate[$i][2];
}
$totalprice=0;
$totalw=0;
if($_POST['pid'] && ($_POST['act']=="add" || $_POST['act']=="adddt" )){
	$add_q=$_SESSION["ses_cart"][$_POST['pid']]["quantity"]+$_POST['quantity'];
	$add_qdt=$_SESSION["ses_cart"][$_POST['pid']]["quantity"];
	$_POST["quantity"]=floor($_POST["quantity"]);
if(($add_q<=$_POST['stock'] && $_POST['act']=="add") || 
( $_POST["quantity"]>0 && is_numeric ($_POST["quantity"]) && $_POST["quantity"]<=$_POST['stock'] && $_POST['act']=="adddt"))
	{
	$_SESSION["ses_cart"][$_POST['pid']]["id"]=$_POST['pid'];
	$_SESSION["ses_cart"][$_POST['pid']]["name"]=$_POST['name'];
	$_SESSION["ses_cart"][$_POST['pid']]["stock"]=$_POST['stock'];
		if($_POST['act']=="add"){
			$_SESSION["ses_cart"][$_POST['pid']]["price"]+=$_POST['price'];
			$_SESSION["ses_cart"][$_POST['pid']]["quantity"]+=$_POST['quantity'];
		}
		else
		{
			$_SESSION["ses_cart"][$_POST['pid']]["price"]=$_POST['price']*$_POST['quantity'];
			$_SESSION["ses_cart"][$_POST['pid']]["quantity"]=$_POST['quantity'];
		}
	$_SESSION["ses_cart"][$_POST['pid']]["weight"]=$_SESSION["ses_cart"][$_POST['pid']]["quantity"]*$_POST['weight'];
	$_SESSION["ses_cart"][$_POST['pid']]["cost"]=$_POST['price'];
	$_SESSION["ses_cart"][$_POST['pid']]["g"]=$_POST['weight'];
	}
	
}
else if($_POST['pid'] && $_POST['act']=="del")
{
unset($_SESSION["ses_cart"][$_POST['pid']]);
}
else if($_POST['dlvid'] && $_POST['act']=="deli_chk")
{
	$_SESSION["curr_deli_chk"]=$_POST['dlvid'];
	$_SESSION["curr_deli_bw"]=$_POST['bw'];
	$_SESSION["curr_deli_sc"]=$_POST['sc'];
}
//print_r($_SESSION["ses_cart"]);
//session_destroy();
?>

<dl class="products">
<? 
if($_SESSION["ses_cart"]){
foreach($_SESSION["ses_cart"] as $product){ 
$totalprice+=$product['price'];
$totalw+=$product['weight'];
?>

<dt class="first_item" id="cart_block_product_{$proid}">
            <input type="hidden" id="{$proid}_orderqr" 	value="$qntt" >
				<span class="quantity-formated"><span class="quantity" style="font-size:12px"><?=$product['quantity'];?></span>x</span>
				<a title="<?=$product['name']; ?>" href="productdetail.php?product=<?=EC("product",$product["id"]);?>" class="cart_block_product_name" style="font-size:12px">
        	    <?=subtext($product['name'],10); ?>
          	    </a>
				<span class="price"><?=format_number($product['price']); ?> ฿</span><span class="remove_link">
                <input type="image"  title="<?=$product['name']; ?>" onclick="delCart(<?=$product["id"];?>,'<?=$product["name"];?>')" name="imageField" id="imageField" src="img/icon/delete.gif" />
                </span>
			</dt>
<? } }  else { ?>
		 <dt class="first_item" >
         <span style="font-size:12px">ไม่มีสินค้า</span>
		 </dt>
	<?	 }?>	
			<!-- Customizable datas -->				
    	</dl>
		<p id="cart-prices">
<span style="text-align:left">จัดส่งโดย</span><br />
<?   
$sqldeli="SELECT * FROM sme_delivery_type WHERE showStatus =1";
$deli= mysql_query($sqldeli);
$i=0;
$strchck="";
while($rsd=mysql_fetch_array($deli)){
	$i++;
	if($_SESSION["curr_deli_chk"])
	{
		if($_SESSION["curr_deli_chk"]==$rsd['delivery_Id'])$strchck="checked=\"checked\"";
		else $strchck="";
	}
	else
	{
		if($i==1)
		{
			$strchck="checked=\"checked\"";
			$_SESSION["curr_deli_chk"]=$rsd['delivery_Id'];
		}
		else $strchck="";
	}
?>
<span style="text-align:left">
<input type="radio" name="radio" id="radio<?=$rsd['delivery_Id']?>"   <?=$strchck?>  onclick="deliv(<?=$rsd['delivery_Id']?>,<?=$rsd['box_weight']?>,<?=$rsd['service_charge']?>)"  />
<?=iconv( 'TIS-620', 'UTF-8', $rsd['delivery_name']);?>
<? if(strlen($rsd['delivery_name'])<=3)echo "&nbsp;&nbsp;&nbsp;"; ?> 
</span><br />
<? } ?>
<? 
if(!empty($_SESSION["ses_cart"]))
$delivP= calcDalivery($totalw,$_SESSION["curr_deli_chk"]); 
else  $delivP=0;
$_SESSION["delivPrice"]=$delivP;
?>
</p>
<p id="cart-prices">
			<span>ค่าสินค้า</span>
			<span class="price ajax_cart_shipping_cost" ><?=format_number($totalprice);?> ฿</span>
			<br>
            <span>ค่าจัดส่ง</span>
			<span class="price ajax_cart_shipping_cost" ><?=format_number($delivP)?> ฿</span>
			<br>
						<span>รวม</span>
			<span class="price ajax_cart_shipping_cost" ><?=format_number( $totalprice+$delivP);?> ฿</span>
		</p>
						<p id="cart-buttons">
			<a title="ตะกร้าสินค้า" class="button_small"  href="cartdetail.php">ตะกร้าสินค้า</a>
            <? 
			if($_SESSION['customname'])
			{
				if($_SESSION["ses_cart"])
				{
				$lstr="href=\"cartdetail.php?order=".date("siHdmY")."\"" ;
				}
				else $lstr="href=\"\" onclick=\"warnEmp()\"";
			}
			else $lstr="href=\"\" onclick=\"warn()\""; ?>
			<a title="สั่งซื้อ" class="exclusive" id="button_order_cart"  <?=$lstr?> >สั่งซื้อ</a>
		</p>